package org.dromara.fms.service.impl;

import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.fms.domain.bo.FmsAccountBo;
import org.dromara.fms.domain.vo.FmsAccountVo;
import org.dromara.fms.domain.FmsAccount;
import org.dromara.fms.mapper.FmsAccountMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 账户信息Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class FmsAccountServiceImpl extends BaseService {

    private final FmsAccountMapper baseMapper;

    /**
     * 查询账户信息
     */
    public FmsAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询账户信息列表
     */
    public TableDataInfo<FmsAccountVo> queryPageList(FmsAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FmsAccount> lqw = buildQueryWrapper(bo);
        Page<FmsAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询账户信息列表
     */
    public List<FmsAccountVo> queryList(FmsAccountBo bo) {
        LambdaQueryWrapper<FmsAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FmsAccount> buildQueryWrapper(FmsAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FmsAccount> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), FmsAccount::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountNo()), FmsAccount::getAccountNo, bo.getAccountNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), FmsAccount::getStatus, bo.getStatus());
        lqw.eq(bo.getPartnerId() != null, FmsAccount::getPartnerId, bo.getPartnerId());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
          FmsAccount::getUpdateTime,params.get("beginUpdateTime"),params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增账户信息
     */
    public Boolean insertByBo(FmsAccountBo bo) {
        FmsAccount add = MapstructUtils.convert(bo, FmsAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改账户信息
     */
    public Boolean updateByBo(FmsAccountBo bo) {
        FmsAccount update = MapstructUtils.convert(bo, FmsAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FmsAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除账户信息
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
      return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据id和实收金额，更新账户余额信息
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public R<Void> updateBalance(Long id, Double money) {
      FmsAccountVo fmsAccountVo = this.queryById(id);
      // 账户状态校验
      if (!fmsAccountVo.getStatus().equals("0")) {
        return R.fail("账户状态未启用！");
      }
      // 账户余额校验
      Double balance = fmsAccountVo.getBalance();
      if (balance + money < 0) {
        return R.fail("账户余额不足！");
      }
      baseMapper.updateBalanceByIdAndMoney(id, money);
      return R.ok();
    }
}
